1716A - 2-3 Moves - CodeForces Solution


greedy math

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    n = int(input())
    print(n//3 + (n%3!=0) + (n==1))

C++ Code:

#pragma GCC optimize("O3")
#include <iostream>
// #include <vector>
// #include <cstring>
#include <algorithm>
// #include <iomanip>
// #include <math.h>
// #include <iomanip>
// #include <map>
// #include <unordered_map>
// #include <set>
// #include <unordered_set>
// #include <stack>
// #include <queue>
// #include <deque>
using namespace std;

template < typename A, typename B > ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << '\n'; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define PI 3.141592653589793116
// PI = acos(-1)
#define ll long long
#define ld long double
#define fi first
#define se second
#define nl '\n'
#define all(x) x.begin(), x.end()
#define gg(t) cout << "Case #"<< t << ": ";
#define line cout << nl;
#define pline cout << nl << "______________________" << nl;
#define f(i, k, n) for(int i = k; i <= n; i++)
#define ms(x, a, n) f(i, 0, n-1) x[i] = a;

#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vvl vector<vector<ll>>

#define cia(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i];
#define ciap(a, n, s) for (int i = s; i < n + s; ++i) cin >> a[i].fi >> a[i].se;
#define ciab(a, b, n, s) for (int i = s; i < n + s; ++i) cin >> a[i] >> b[i];
#define cim(a, m, n, s) for (int i = s; i < m + s; ++i) for (int j = s; j < n + s; ++j) cin >> a[i][j];
#define tr(it, a) for(auto &it : a)
#define sz(x) int((x).size())

const ll mod = 1e9 + 7;
const int inf = 2e9;
const int lim = 1e6 + 5;

ll n, cnt1, cnt2, cnt3;

void run_case()
{
    cnt1 = cnt2 = cnt3 = inf;
    cin >> n;
    if (n % 2 == 0)
        cnt1 = n / 2;
    if (n % 3 == 0)
        cnt2 = n / 3;
    else if (n % 3 == 1)
    {
        cnt2 = 2;
        n -= 4;
        cnt2 += abs(n / 3);
        n += 4;
        cnt3 = 1;
        n += 2;
        cnt3 += abs(n / 3);
    }
    else if (n % 3 == 2)
    {
        cnt2 = 1;
        n -= 2;
        cnt2 += (n / 3);
    }
    cout << min({cnt1, cnt2, cnt3}) << nl;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    // freopen("A.INP", "r", stdin);
    // freopen("A.OUT", "w", stdout);
    int t = 1;
    cin >> t;
    f(i, 1, t)
    {
        // gg(i);
        run_case();
    }
}


Comments

Submit
0 Comments
More Questions

1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number